package neo.tetris.rpc.session;

import java.nio.ByteBuffer;
import java.util.Vector;


public class SessionGroup {
	private Vector<SessionModel> obs;

	public SessionGroup() {
		obs = new Vector<SessionModel>();
	}

	public synchronized void addObserver(SessionModel o) {
		if (o == null)
			throw new NullPointerException();
		if (!obs.contains(o)) {
			obs.addElement(o);
		} else {
			throw new Error("duplicate");
		}
	}

	public synchronized void deleteObserver(SessionModel o) {
		obs.removeElement(o);
	}

	public void write(ByteBuffer buffer) {
		SessionModel[] observers;

		synchronized (this) {
			observers = obs.toArray(new SessionModel[] {});
		}

		for (int i = observers.length - 1; i >= 0; i--) {
			observers[i].write(buffer);
		}
	}
}
